//
//  ViewController.m
//  Package
//
//  Created by Juvid on 16/6/1.
//  Copyright © 2016年 Juvid. All rights reserved.
//

#import "ViewController.h"
#define KeyVersion @"version"
#define KeyBuild   @"build"
#define KeyProjectName @"projectName"
#define FilePath @"filePath"
@interface ViewController (){
    
    __weak IBOutlet NSTextField *txtSelect;
    __weak IBOutlet NSTextField *txtBuild;
    __weak IBOutlet NSTextField *txtVersion;
    __weak IBOutlet NSTextField *txtProject;
    NSMutableDictionary *dicVersion;
    NSString *strFilePath;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    dicVersion=[NSMutableDictionary dictionaryWithContentsOfFile:[self versionPath]];
    if (dicVersion) {
        txtVersion.stringValue=dicVersion[KeyVersion];
        txtBuild.stringValue=dicVersion[KeyBuild];
        txtProject.stringValue=dicVersion[KeyProjectName];
        if (dicVersion[FilePath]) {
            txtSelect.stringValue = dicVersion[FilePath];
        }
    }
     strFilePath=txtSelect.stringValue;
    if (!dicVersion) {
        dicVersion=[NSMutableDictionary dictionary];
    }
    // Do any additional setup after loading the view.
}
- (IBAction)ClickTest:(id)sender {
    [self JuGetFileName:@"beta"];
}
- (IBAction)ClickTestRelease:(id)sender {
     [self JuGetFileName:@"preview"];
}
- (IBAction)ClickRelease:(id)sender {
     [self JuGetFileName:@"publish"];
}
- (IBAction)ClickSelect:(NSButton *)sender {
    NSOpenPanel *panel = [NSOpenPanel openPanel];
    NSArray* fileTypes = [[NSArray alloc] initWithObjects:@"txt",@"doc",@"ipa", nil];
    [panel setPrompt:@"确定"];
    [panel setCanChooseDirectories:NO];
    [panel setCanCreateDirectories:YES];
    [panel setCanChooseFiles:YES];
    [panel setAllowsMultipleSelection:YES];
    
    [panel setAllowedFileTypes:fileTypes];
//    strFilePath=@"";
    NSArray *select_files;
    NSInteger result = [panel runModal];
//    txtSelect.stringValue =@"";
    if (result ==NSFileHandlingPanelOKButton)
    {
        select_files = [panel URLs] ;
        for (int i=0; i<select_files.count; i++)
        {
            NSURL *Url= [select_files objectAtIndex:i];
            strFilePath=Url.resourceSpecifier;
            txtSelect.stringValue = strFilePath;
            txtSelect.stringValue = [txtSelect.stringValue stringByAppendingString:@"\n"];
             txtProject.stringValue=[[strFilePath lastPathComponent] stringByDeletingPathExtension];
            NSLog(@"%@",strFilePath);
        }
    }
   
    
}
-(void)reName:(NSString *)fileName{
//    对一个文件重命名
//    想要重命名一个文件，我们需要把文件移到一个新的路径下。下面的代码创建了我们所期望的目标文件的路径，然后请求移动文件以及在移动之后显示文件目录。
    //通过移动该文件对文件重命名
    if (!strFilePath||[strFilePath isEqual:@""]) {
         [fileName  writeToFile:[NSString stringWithFormat:@"%@%@",[self shCreateDirectory],fileName] atomically:YES encoding:NSUTF8StringEncoding error:nil];
        return;
    }
    
    NSFileManager *fileMgr = [NSFileManager defaultManager];
    NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDesktopDirectory,NSUserDomainMask, YES);
    NSString *path_Desktop =[NSString stringWithFormat:@"%@",[paths objectAtIndex:0]];
    NSString *filePath2= [path_Desktop stringByAppendingPathComponent:fileName];
   
    //判断是否移动
    NSError *error;
//    if ([fileMgr moveItemAtPath:strFilePath toPath:filePath2 error:&error] != YES)
//         if ([fileMgr moveItemAtPath:filePath2 toPath:strFilePath error:&error] != YES)
//        NSLog(@"Unable to move file: %@", [error localizedDescription]);
    
//    [fileMgr createDirectoryAtPath:[filePath2 stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:nil];
    
    [fileMgr copyItemAtPath:strFilePath toPath:filePath2 error:&error];
    

    
    //显示文件目录的内容
   
}
-(void)JuGetFileName:(NSString *)lastName{
//    NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDesktopDirectory,NSUserDomainMask, YES);
//    [NSString stringWithFormat:@"%@/Model/",[paths objectAtIndex:0]];
    [dicVersion setValue:txtProject.stringValue forKey:KeyProjectName];
    [dicVersion setValue:txtBuild.stringValue forKey:KeyBuild];
    [dicVersion setValue:txtVersion.stringValue forKey:KeyVersion];
    [dicVersion setValue:strFilePath forKey:FilePath];
    BOOL Write=[dicVersion writeToFile:[self versionPath] atomically:YES];
// [txtVersion  writeToFile:[NSString stringWithFormat:@"%@%@",[self shCreateDirectory],fileName] atomically:YES encoding:NSUTF8StringEncoding error:nil];
   
    NSString*strName= [NSString stringWithFormat:@"%@_%@_%@_ios_%@_%@.ipa",txtProject.stringValue,txtVersion.stringValue,txtBuild.stringValue,[self JuGetDate],lastName];
    [self reName:strName];
}
-(NSString *)versionPath{
    return [NSString stringWithFormat:@"%@version.plist",[self shCreateDirectory]];
}
-(NSString *)shCreateDirectory{
    NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDesktopDirectory,NSUserDomainMask, YES);
   NSString *path_Desktop =[NSString stringWithFormat:@"%@/IPA/",[paths objectAtIndex:0]];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if (![fileManager fileExistsAtPath:path_Desktop])
    {
        [fileManager createDirectoryAtPath:path_Desktop
               withIntermediateDirectories:YES
                                attributes:nil
                                     error:NULL];
    }
    return path_Desktop;
}
-(NSString *)JuGetDate{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyyMMdd"];
    return  [dateFormatter stringFromDate:[NSDate date]];
}
- (void)setRepresentedObject:(id)representedObject {
    [super setRepresentedObject:representedObject];

    // Update the view, if already loaded.
}

@end
